#include "ParameterValues.h"
#include "malloc.h"
#include "string.h"
#include "stdlib.h"

void DefineParamValueI(ParamValue* pv, int type, int size, const int* address)
{
	pv->type = type;
	pv->size = size;
	pv->val_mat_int = (int *)address;
}
void DefineParamValueS(ParamValue* pv, int type, int size, const char** address)
{
	(*pv).type = type;
	(*pv).size = size;
	(*pv).val_mat_str = address;
}

#define UI_MODE_PORT_size 6*2
const char* UI_MODE_PORT_tbl[UI_MODE_PORT_size] = {
		"0", "Reserved",
		"1", "ASYNC",
		"2", "SPI",
		"3", "PWM",
		"4", "UDEF",
		"5", "ADC",
};
/*
#define UI_ASYNC_BAUD_size 10
const int UI_ASYNC_BAUD_tbl[UI_ASYNC_BAUD_size] =
{
		1200,
		2400,
		4800,
		9600,
		19200,
		38400,
		57600,
		115200,
		230400,
};


#define UI_ASYNC_PARITY_size 6
const char* UI_ASYNC_PARITY_tbl[UI_ASYNC_PARITY_size] =
{
		"0", "NONE",
		"1", "ODD",
		"2", "EVEN",
};

#define UI_ASYNC_DATA_size 7
const int UI_ASYNC_DATA_tbl[UI_ASYNC_DATA_size] =
{
		5,
		6,
		7,
		8,
};

#define UI_ASYNC_STOP_size 6
const char* UI_ASYNC_STOP_tbl[UI_ASYNC_STOP_size] =
{
		"0", "1",
		"1", "1.5",
		"2", "2",
};

#define UI_ASYNC_FLOW_size 4
const char* UI_ASYNC_FLOW_tbl[UI_ASYNC_FLOW_size] =
{
		"0", "NONE",
		"1", "HARDWARE"
};
*/
#define UI_SPI_BAUD_size 31
const int UI_SPI_BAUD_tbl[UI_SPI_BAUD_size] =
{
		500,
		1000,
		1500,
		2000,
		2500,
		3000,
		3500,
		4000,
		4500,
		5000,
		5500,
		6000,
		6500,
		7000,
		7500,
		8000,
		8500,
		9000,
		9500,
		10000,
		10500,
		11000,
		11500,
		12000,
		12500,
		13000,
		13500,
		14000,
		14500,
		15000,
		15500,
};

#define UI_SPI_DATA_size 4
const int UI_SPI_DATA_tbl[UI_SPI_DATA_size] =
{
		-1,
		8,
		16,
		32,
};

#define WL_LINK_MOD_size 9*2
const char* WL_LINK_MOD_tbl[WL_LINK_MOD_size] =
{
		"1", "4QAM",
		"2", "16QAM",
		"3", "64QAM",
		"4", "2FSK",
		"5", "4FSK",
		"6", "GMSK",
		"7", "4QAM",
		"8", "16QAM",
		"9", "64QAM",
};

#define WL_LINK_FEC_size 6*2
//#define WL_LINK_FEC_size 7*2
const char* WL_LINK_FEC_tbl[WL_LINK_FEC_size] =
{
		"0", "Off",
		"1", "Hamming(7,4)",
		"2", "TMS_Code",
		"3", "RS_45_33",
		"4", "RS_120_88",
		"5", "RS_120_104"//,
		//"15", "Automatic"
};
#define WL_LINK_PROT_size 4*4
const char* WL_LINK_PROT_tbl[WL_LINK_PROT_size] =
{
		"1", "Transparent", "15", "4",
		"2", "Addressed", "15", "4",
		"12", "Transparent w/EOT", "15", "4",
		"15", "Satel 3AS", "15", "4"
};
#define WL_LINK_SPACE_size 6
const char* WL_LINK_SPACE_tbl[WL_LINK_SPACE_size] =
{
		"0", "9600",
		"1", "4800",
		"2", "2400"
};
#define WL_LINK_BAUD_size 9*2
const char* WL_LINK_BAUD_tbl[WL_LINK_BAUD_size] =
{
		"0", "9600",
		"1", "4800",
		"2", "2400",
		"3", "4000",
		"4", "7200",
		"5", "8000",
		"6", "12000",
		"7", "14400",
		"8", "16000"
};
#define WL_LINK_PWR_size 6*2
const char* WL_LINK_PWR_tbl[WL_LINK_PWR_size] =
{
		"27", "54",
		"29", "58",
		"31", "62",
		"33", "66",
		"35", "70",
		"37", "74"
};
/*
#define WL_WIFI_MODE_size 2
#define WL_WIFI_MODE_len  4
unsigned const char WL_WIFI_MODE_tbl[WL_WIFI_MODE_size][WL_WIFI_MODE_len] =
{
		"WAP",
		"P2P",
};
 */
#define NET_SECUR_SECTYPE_size 7*2
const char* NET_SECUR_SECTYPE_tbl[NET_SECUR_SECTYPE_size] =
{
		"0", "OpenSystem",
		"1", "SharedKey",
		"2", "WPA2_Personal",
		"3", "WPA_Personal",
		"4", "WPA2_Enterprise",
		"5", "WPA_Enterprise",
		"6", "802.1x"
};
#define NET_SECUR_ENCTYPE_size 3*2
const char* NET_SECUR_ENCTYPE_tbl[NET_SECUR_ENCTYPE_size] =
{
		"0", "WEP",
		"1", "AES",
		"2", "TKIP"
};


#define OPS_PWRS_MODE_size 8*3
const char* OPS_PWRS_MODE_tbl[OPS_PWRS_MODE_size] =
{
		"0", "Disable Sleep mode", "0",
		"1", "Wakeup by Timer", "3",
		"2", "Wakeup by toggling on DTR input control line", "5",
		"3", "Wakeup by Timer and/or toggling on DTR input control line", "7",
		"4", "Wakeup by USB port", "9",
		"5", "Wakeup by Timer and/or USB port", "11",
		"6", "Wakeup by USB port and/or \r\n\t\t      toggling on DTR input control line", "13",
		"7", "Wakeup by Timer and/or USB port and/or \r\n\t\t      toggling on DTR input control line", "15"
};

#define ADV_DPORT_PRIME_size 2*2
const char* ADV_DPORT_PRIME_tbl[ADV_DPORT_PRIME_size] =
{
		"0", "UART",
		"1", "USB Device"
};

#define ADV_DPORT_ECHO_size 2*2
const char* ADV_DPORT_ECHO_tbl[ADV_DPORT_ECHO_size] =
{
		"0", "Echo Off",
		"1", "Echo On"
};

#define ADV_DPORT_RATE_size 10*2
const char* ADV_DPORT_RATE_tbl[ADV_DPORT_RATE_size] =
{
		"1", "1200",
		"2", "2400",
		"3", "4800",
		"4", "9600",
		"5", "14400",
		"6", "19200",
		"7", "38400",
		"8", "57600",
		"9", "115200",
		"10", "230400"
};

#define ADV_DPORT_BITS_size 4*2
const char* ADV_DPORT_BITS_tbl[ADV_DPORT_BITS_size] =
{
		"0", "8",
		"1", "7",
		"2", "6",
		"3", "5"
};

#define ADV_DPORT_PARITY_size 3*3
const char* ADV_DPORT_PARITY_tbl[ADV_DPORT_PARITY_size] =
{
		"0", "None", "1",
		"1", "Odd", "3",
		"2", "Even", "2"
};

#define ADV_DPORT_FLOW_size 3*3
const char* ADV_DPORT_FLOW_tbl[ADV_DPORT_FLOW_size] =
{
		"0", "None", "1",
		"1", "Xon / Xoff", "3",
		"2", "Hardware (RTS/CTS)", "2"
};

#define ADV_DPORT_STOP_size 3*3
const char* ADV_DPORT_STOP_tbl[ADV_DPORT_STOP_size] =
{
		"0", "1", "2",
		"1", "1.5", "3",
		"2", "2", "4"
};


#define ADV_XMOD_IMAGE_size 8*2
const char* ADV_XMOD_IMAGE_tbl[ADV_XMOD_IMAGE_size] =
{
		"0", "",
		"1", "",
		"11", "GMSK Modulation 1", //2*2+1=5
		"12", "GMSK Modulation 2", //5+2=7
		"21", "MFSK Modulation 1", //7+2=9
		"22", "MFSK Modulation 2", //9+2=11
		"41", "MQAM Modulation 1", //11+2=13
		"42", "MQAM Modulation 2"  //13+2=15
};
#define ADV_XMOD_CFGDN_size 3*2
const char* ADV_XMOD_CFGDN_tbl[ADV_XMOD_CFGDN_size] =
{
		"0", "User Configuration", //0*2+1=1
		"31", "Factory Configuration",//1+2=3
		"32", "Calibration Table"//3+2=5
};

#define ADV_XMOD_CFGUP_size 2*2
const char* ADV_XMOD_CFGUP_tbl[ADV_XMOD_CFGUP_size] =
{
		"0", "User Configuration",//1
		"31", "Factory Configuration"//3
};


#define ADV_TEST_TSTSGL_size 6*2
const char* ADV_TEST_TSTSGL_tbl[ADV_TEST_TSTSGL_size] =
{
		"0", "stops the testing signal generation",
		"1", "Continuous Wave (CW) or not-modulated signal",
		"2", "Continuous bit streams of all 1-s (4QAM modulated)",
		"3", "Continuous Pseudo-Random bit sequence \r\n\t\t      of at least 511 bits",
		"4", "Continuous Double Sideband signal (GMSK modulated)",
		"5", "Continuous Single Sideband (SSB) signal"
};
/*
#define OPS_PWRS_TIMER_size 2
#define OPS_PWRS_TIMER_len   4
unsigned const char OPS_PWRS_TIMER_tbl[OPS_PWRS_TIMER_size][OPS_PWRS_TIMER_len] =
{
		"OFF",
		"ON",
};

#define OPS_PWRS_UART_size 2
#define OPS_PWRS_UART_len  4
unsigned const char OPS_PWRS_UART_tbl[OPS_PWRS_UART_size][OPS_PWRS_UART_len] =
{
		"OFF",
		"ON",
};

#define OPS_PWRS_PIN_size 2
#define OPS_PWRS_PIN_len  4
unsigned const char OPS_PWRS_PIN_tbl[OPS_PWRS_PIN_size][OPS_PWRS_PIN_len] =
{
		"OFF",
		"ON",
};

#define OPS_PWRS_WIFI_size 2
#define OPS_PWRS_WIFI_len  4
unsigned const char OPS_PWRS_WIFI_tbl[OPS_PWRS_WIFI_size][OPS_PWRS_WIFI_len] =
{
		"OFF",
		"ON",
};

#define OPS_PWRS_UHF_size 2
#define OPS_PWRS_UHF_len  4
unsigned const char OPS_PWRS_UHF_tbl[OPS_PWRS_UHF_size][OPS_PWRS_UHF_len] =
{
		"OFF",
		"ON",
};

#define OPS_PWRS_USB_size 2
#define OPS_PWRS_USB_len  4
unsigned const char OPS_PWRS_USB_tbl[OPS_PWRS_USB_size][OPS_PWRS_USB_len] =
{
		"OFF",
		"ON",
};

#define OPS_PWRS_BT_size 2
#define OPS_PWRS_BT_len  4
unsigned const char OPS_PWRS_BT_tbl[OPS_PWRS_BT_size][OPS_PWRS_BT_len] =
{
		"OFF",
		"ON",
};

#define OPS_PWRS_LVL_size 2
const int OPS_PWRS_LVL_tbl[OPS_PWRS_LVL_size] =
{
		0,
		1,
};
*/
/*
#define NET_DL_MNG_size 2
#define NET_DL_MNG_len  7
unsigned const char NET_DL_MNG_tbl[NET_DL_MNG_size][NET_DL_MNG_len] =
{
		"Master",
		"Slave"
};

#define NET_APP_HTTPD_size 2
#define NET_APP_HTTPD_len  4
unsigned const char NET_APP_HTTPD_tbl[NET_APP_HTTPD_size][NET_APP_HTTPD_len] =
{
		"OFF",
		"ON",
};

#define NET_APP_TRANS_size 2
#define NET_APP_TRANS_len  4
unsigned const char NET_APP_TRANS_tbl[NET_APP_TRANS_size][NET_APP_TRANS_len] =
{
		"OFF",
		"ON",
};
*/

void Init_Param_Values()
{
	//DefineParamValueI(&UI_ASYNC_BAUD, V_INT, UI_ASYNC_BAUD_size, UI_ASYNC_BAUD_tbl);
	//DefineParamValueI(&UI_ASYNC_DATA, V_INT, UI_ASYNC_DATA_size, UI_ASYNC_DATA_tbl);
	DefineParamValueI(&UI_SPI_BAUD, V_INT, UI_SPI_BAUD_size, UI_SPI_BAUD_tbl);
	DefineParamValueI(&UI_SPI_DATA, V_INT, UI_SPI_DATA_size, UI_SPI_DATA_tbl);
	//DefineParamValueI(&OPS_PWRS_LVL, V_INT, OPS_PWRS_LVL_size, OPS_PWRS_LVL_tbl);
	//DefineParamValueS(&UI_ASYNC_PARITY, V_STR, UI_ASYNC_PARITY_size, (const char**)UI_ASYNC_PARITY_tbl);
	//DefineParamValueS(&UI_ASYNC_STOP, V_STR, UI_ASYNC_STOP_size, (const char**)UI_ASYNC_STOP_tbl);
	//DefineParamValueS(&UI_ASYNC_FLOW, V_STR, UI_ASYNC_FLOW_size, 	(const char**)UI_ASYNC_FLOW_tbl);
	DefineParamValueS(&WL_LINK_MOD, V_STR, WL_LINK_MOD_size, 		(const char**)WL_LINK_MOD_tbl);
	DefineParamValueS(&WL_LINK_FEC, V_STR, WL_LINK_FEC_size, 		(const char**)WL_LINK_FEC_tbl);
	DefineParamValueS(&WL_LINK_PROT, V_STR_REG, WL_LINK_PROT_size, 		(const char**)WL_LINK_PROT_tbl);
	DefineParamValueS(&WL_LINK_SPACE, V_STR, WL_LINK_SPACE_size, 		(const char**)WL_LINK_SPACE_tbl);
	DefineParamValueS(&WL_LINK_BAUD, V_STR, WL_LINK_BAUD_size, 		(const char**)WL_LINK_BAUD_tbl);
	DefineParamValueS(&WL_LINK_PWR, V_STR, WL_LINK_PWR_size, 		(const char**)WL_LINK_PWR_tbl);
	//DefineParamValueS(&WL_WIFI_MODE, V_STR, WL_WIFI_MODE_size, 		(unsigned const char**)WL_WIFI_MODE_tbl);
	DefineParamValueS(&NET_SECUR_SECTYPE, V_STR, NET_SECUR_SECTYPE_size,(const char**)NET_SECUR_SECTYPE_tbl);
	DefineParamValueS(&NET_SECUR_ENCTYPE, V_STR, NET_SECUR_ENCTYPE_size,(const char**)NET_SECUR_ENCTYPE_tbl);
	//DefineParamValueS(&NET_APP_TRANS, V_STR, NET_APP_TRANS_size,	(unsigned const char**)NET_APP_TRANS_tbl);
	//DefineParamValueS(&NET_APP_HTTPD, V_STR, NET_APP_HTTPD_size, 	(unsigned const char**)NET_APP_HTTPD_tbl);
	//DefineParamValueS(&NET_DL_MNG, V_STR, NET_DL_MNG_size, 			(unsigned const char**)NET_DL_MNG_tbl);
	DefineParamValueS(&UI_MODE_PORT, V_STR, UI_MODE_PORT_size, 		(const char**)UI_MODE_PORT_tbl);
	/*
	DefineParamValueS(&OPS_PWRS_BT, V_STR,OPS_PWRS_BT_size, 		(unsigned const char**)OPS_PWRS_BT_tbl);
	DefineParamValueS(&OPS_PWRS_USB, V_STR, OPS_PWRS_USB_size, 		(unsigned const char**)OPS_PWRS_USB_tbl);
	DefineParamValueS(&OPS_PWRS_UHF, V_STR, OPS_PWRS_UHF_size, 		(unsigned const char**)OPS_PWRS_UHF_tbl);
	DefineParamValueS(&OPS_PWRS_WIFI, V_STR, OPS_PWRS_WIFI_size, 	(unsigned const char**)OPS_PWRS_WIFI_tbl);
	DefineParamValueS(&OPS_PWRS_PIN, V_STR, OPS_PWRS_PIN_size, 		(unsigned const char**)OPS_PWRS_PIN_tbl);
	DefineParamValueS(&OPS_PWRS_UART, V_STR, OPS_PWRS_UART_size, 	(unsigned const char**)OPS_PWRS_UART_tbl);
	DefineParamValueS(&OPS_PWRS_TIMER, V_STR, OPS_PWRS_TIMER_size, 	(unsigned const char**)OPS_PWRS_TIMER_tbl);
	*/
	DefineParamValueS(&OPS_PWRS_MODE, V_STR_INT, OPS_PWRS_MODE_size, 	(const char**)OPS_PWRS_MODE_tbl);
	DefineParamValueS(&ADV_DPORT_PRIME, V_STR, ADV_DPORT_PRIME_size, 	(const char**)ADV_DPORT_PRIME_tbl);
	DefineParamValueS(&ADV_DPORT_ECHO, V_STR, ADV_DPORT_ECHO_size, 	(const char**)ADV_DPORT_ECHO_tbl);
	DefineParamValueS(&ADV_DPORT_RATE, V_STR, ADV_DPORT_RATE_size, 	(const char**)ADV_DPORT_RATE_tbl);
	DefineParamValueS(&ADV_DPORT_BITS, V_STR, ADV_DPORT_BITS_size, 	(const char**)ADV_DPORT_BITS_tbl);
	DefineParamValueS(&ADV_DPORT_PARITY, V_STR_INT, ADV_DPORT_PARITY_size, 	(const char**)ADV_DPORT_PARITY_tbl);
	DefineParamValueS(&ADV_DPORT_FLOW, V_STR_INT, ADV_DPORT_FLOW_size, 	(const char**)ADV_DPORT_FLOW_tbl);
	DefineParamValueS(&ADV_DPORT_STOP, V_STR_INT, ADV_DPORT_STOP_size, 	(const char**)ADV_DPORT_STOP_tbl);
	DefineParamValueS(&ADV_XMOD_IMAGE, V_STR, ADV_XMOD_IMAGE_size, 	(const char**)ADV_XMOD_IMAGE_tbl);
	DefineParamValueS(&ADV_XMOD_CFGDN, V_STR, ADV_XMOD_CFGDN_size, 	(const char**)ADV_XMOD_CFGDN_tbl);
	DefineParamValueS(&ADV_XMOD_CFGUP, V_STR, ADV_XMOD_CFGUP_size, 	(const char**)ADV_XMOD_CFGUP_tbl);
	DefineParamValueS(&ADV_TEST_TSTSGL, V_STR, ADV_TEST_TSTSGL_size, 	(const char**)ADV_TEST_TSTSGL_tbl);
}
/*

int GetValueIndexI(ParamValue* pv, void* value)
{
	int val_i;
	int size = (*pv).size;
	int i;

	if((*pv).type == V_INT)
	{
		val_i = *((int*)value);
		for(i = 0; i < size; ++i)
			if( ((*pv).val_mat_int)[i] == val_i)
				return i;
	}
	return -1;
}

int GetValueIndexS(ParamValue* pv, char* value)
{
	char* val_s;
	int size = (*pv).size, len = (*pv).str_len;
	int i;
	int cond;

	if ((*pv).type == V_STR)
	{
		val_s = value;
		for(i = 0; i < size; ++i)
		{
			cond = 1;
			int offset = i * len;
			//int next = (i + 1) * len;
			unsigned char * t;
			t= (unsigned char *)(&((((unsigned char *)(&(((*pv).val_mat_str)[0]))) + offset)[0]));

			if(!strcmp((char *)t, val_s))
				return i;
		}
	}
	return -1;
}

*/

int GetAbsoluteIndex(ParamValue* pv, const char value_index)
{
	int size = (*pv).size;
	int i = 0;
	for(; i < size; i += pv->type)
	{
		if(value_index == atoi((char*)pv->val_mat_str[i]))
		{
			if(pv->type == V_STR_REG)
				return i+1;
			else
				return i+pv->type-1;
		}
	}
	return -1;
}

int GetAbsoluteIndex_forREG(ParamValue* pv, const char value_reg)
{
	int size = (*pv).size;
	int i = 0;
	for(; i < size; i += pv->type)
	{
		int ind = atoi((char*)pv->val_mat_str[i]);
		int maska = atoi((char*)pv->val_mat_str[i+2]);
		int l_shift = atoi((char*)pv->val_mat_str[i+3]);
		if(((value_reg & ~maska) >> l_shift) == ind)
			return i+1;
	}
	return -1;
}

int GetAbsoluteIndex_forInt(ParamValue* pv, int value)
{
	int size = (*pv).size;
	int i;
	for(i = pv->type-1; i < size; i += pv->type)
	{
		if(value == atoi((char*)pv->val_mat_str[i]))
			return i-pv->type+1;
	}
	return -1;
}

int GetRoundValue(ParamValue* pv, char source_val, char* dest_val)
{
	int size = (*pv).size;
	int i; int curr_val;
	for(i = size+1-pv->type; i > 0; i -= pv->type)
	{
		curr_val = atoi((char*)pv->val_mat_str[i]);
		if(curr_val <= source_val)
		{
			*dest_val = (char)curr_val;
			return 1;
		}
	}
	return -1;
}
